<template>
  <div>
    <h3 class="ac clfff mgb50 fs20 ls2 fw700">vue3后台管理系统</h3>
    <div class="login-from">
      <el-avatar
        class="logo"
        :size="66"
        src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
      />
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-position="top"
        class="login-message bgfff"
      >
        <el-form-item label="账户" prop="account" size="large">
          <el-input v-model="ruleForm.account" placeholder="请输入您的账号" />
        </el-form-item>
        <el-form-item label="密码" prop="password" size="large">
          <el-input
            v-model="ruleForm.password"
            placeholder="请输入您的密码"
            type="password"
            show-password
            @keyup.enter="submitForm(ruleFormRef)"
          />
        </el-form-item>
        <el-form-item size="large">
          <el-button type="primary" class="submit-btn" round @click="submitForm(ruleFormRef)"
            >进入系统</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import local from '@/utils/local'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import type { FormInstance } from 'element-plus'
import { ElLoading, ElMessage } from 'element-plus'
const { push } = useRouter()

const ruleFormRef = ref<FormInstance>()

const ruleForm = reactive({
  account: '',
  password: ''
})

const rules = reactive({
  account: [
    { required: true, message: '请输入您的账号', trigger: 'blur' },
    { pattern: /^[^\u4e00-\u9fa5]+$/, message: '账号不能输入汉字', trigger: 'blur' }
  ],
  password: [{ required: true, message: '请输入您的密码', trigger: 'blur' }]
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) {
    return
  }
  formEl.validate(async (valid) => {
    if (valid) {
      const loading = ElLoading.service({
        lock: true,
        text: '正在登录中...',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      setTimeout(() => {
        loading.close()
      }, 2000)
      ElMessage({
        message: '登录成功',
        type: 'success'
      })
      local.set('t_k', '123456')
      setTimeout(() => {
        push('/home')
      }, 2000)
    } else {
      return false
    }
  })
}
</script>

<style lang="scss" scoped>
.login-from {
  position: relative;
  border: 1px solid #eee;
  width: 360px;
  box-shadow: 0 0 10px 2px #eee;
  .logo {
    position: absolute;
    left: 150px;
    top: -26px;
    box-shadow: 0 0 5px 10px #fff;
    border: 1px solid #fff;
  }
  .login-message {
    box-sizing: border-box;
    padding: 20px;
    padding-top: 40px;
  }
}
.submit-btn {
  width: 100%;
  letter-spacing: 4px;
}
</style>
